$(function () {

    //获取所有的菜单
    let categoryJson = JEE.getJson("/system/sysDict/listByParentValue", {
        value: "MODEL_TYPE"
    }, "value", "name");

    //定义搜索区域字段
    let searchValues = [
        {
            id: "textSearchKey",
            field: "key",
            title: "模型标识"
        },
        {
            id: "textSearchName",
            field: "name",
            title: "模型名称"
        },
        {
            id: "comboSearchCategory",
            field: "category",
            title: "模型类型",
            comboUrl: "/system/sysDict/listByParentValue",
            comboData: {
                value: "MODEL_TYPE"
            },
            comboId: "value",
            comboText: "name"
        }
    ];

    //初始化搜索区域
    JEE.initSearch({
        panelTitle: "查询",
        searchValues: searchValues,
        defaultTable: "tb_data" //关联表格id
    });

    //初始化表格
    JEE.initTable({
        btnItems: [
            {
                btnId: "btn_add",
                btnCss: "btn",
                btnText: "新增",
                btnClick: function () {
                    showDialog();
                }
            },
            {
                btnId: "btn_edit",
                btnCss: "btn btn-primary",
                btnText: "编辑",
                btnClick: function () {
                    let selections = $("#tb_data").bootstrapTable("getSelections");
                    if (selections.length != 1) {
                        JEE.errMsg("请选择需要修改的数据");
                        return;
                    }

                    console.log("modelId == " + selections[0].id);

                    window.open(oaUrl + "/modeler.html?modelId=" + selections[0].id, "_blank");
                },
                otherOptions: [{
                    id: "tb_data",
                    selectNum: 1
                }]
            },
            {
                btnId: "btn_delete",
                btnCss: "btn btn-danger",
                btnText: "删除",
                otherOptions: [{
                    id: "tb_data",
                    selectMinNum: 1
                }],
                btnClick: function () {
                    let selections = $("#tb_data").bootstrapTable("getSelections");
                    if (selections.length != 1) {
                        JEE.errMsg("请选择需要删除的数据");
                        return;
                    }

                    if (selections[0].deploymentId) {
                        JEE.errMsg("流程模型已部署，无法删除");
                        return;
                    }

                    JEE.confirmMsg("是否确认删除数据？", function () {
                        $.when(JEE.myAjax(oaUrl + "/activity/customModel/deleteModel", {id: selections[0].id})).done(function (res) {
                            if (res) {
                                $("#myModal").modal("hide");
                                $("#tb_data").bootstrapTable("refresh");
                            }
                        });
                        layer.closeAll("dialog");
                    });
                }
            },
            {
                btnId: "btn_deploy",
                btnCss: "btn btn btn-primary",
                btnText: "发布",
                otherOptions: [{
                    id: "tb_data",
                    selectMinNum: 1
                }],
                btnClick: function () {
                    let selections = $("#tb_data").bootstrapTable("getSelections");
                    if (selections.length != 1) {
                        JEE.errMsg("请选择需要发布的流程模型！");
                        return;
                    }

                    JEE.confirmMsg("是否确认发布流程模型？", function () {
                        $.when(JEE.myAjax(oaUrl + "/activity/customModel/deployModel", {id: selections[0].id})).done(function (res) {
                            if (res) {
                                $("#myModal").modal("hide");
                                $("#tb_data").bootstrapTable("refresh");
                            }
                        });
                        layer.closeAll("dialog");
                    });
                }
            }
        ],
        tableId: "tb_data",
        mainSearch: searchValues,
        url: oaUrl + "/activity/customModel/listByPage",
        columns: [
            {
                checkbox: true
            },
            {
                field: "key",
                title: "模型标识"
            },
            {
                field: "name",
                title: "模型名称"
            },
            {
                field: "category",
                title: "模型分类",
                formatter: function (val, row, index) {
                    if (val) {
                        return categoryJson[val];
                    }
                }
            },
            {
                field: "version",
                title: "版本号"
            },
            {
                field: "deploymentId",
                title: "是否布署",
                formatter: function (val, row, index) {
                    if (val) {
                        return '是';
                    }
                    return '否';
                }
            }
        ]
    });
});

//弹出框
function showDialog(change, formData) {
    let defaultTable = change == "edit" ? "tb_data" : null;
    JEE.initDialog({
        modalId: "myModal",
        modalTitle: "新增",
        btnItems: [{
            btnId: "btn_save",
            btnCss: "btn btn-primary",
            btnText: "保存",
            formId: "dataForm",
            //showConfirm:true,
            btnClick: function (data) {
                let url = oaUrl + "/activity/customModel/addModel";
                $.when(JEE.myAjax(url, data)).done(function (result) {
                    if (result) {
                        window.open(oaUrl + "/modeler.html?modelId=" + result, "_blank");
                        $("#myModal").modal("hide");
                        $("#tb_data").bootstrapTable("refresh");
                    }
                })
            }
        }],
        modalForm: {
            formId: "dataForm",
            defaultTable: defaultTable,
            formItems: [
                {
                    id: "textKey",
                    field: "key",
                    title: "流程标识",
                    disable: false,
                    valid: "required"
                },
                {
                    id: "textName",
                    field: "name",
                    title: "流程名称",
                    disable: false,
                    valid: "required"
                },
                {
                    id: "comboCategory",
                    field: "category",
                    title: "流程类型",
                    disable: false,
                    comboUrl: "/system/sysDict/listByParentValue",
                    comboData: {
                        value: "MODEL_TYPE"
                    },
                    comboId: "value",
                    comboText: "name"
                },
                {
                    id: "areaDescription",
                    field: "description",
                    title: "流程描述",
                    disable: false
                }
            ]
        }
    });
}
